package com.example.hello1;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class myadapter_thred_copy implements Runnable {
    private static final String Tag = "单独子线程类接口";
    Handler handler;
    Context context;
    private String logDate = "";
    private final String DATE_SP_KEY = "lastRateDateStr";


    public myadapter_thred_copy(Context ratelistactivitu) {
        this.context=ratelistactivitu;
    }

    public void setHadeler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void run() {


        String curDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
        logDate=curDateStr;
        Log.i(Tag, "curDateStr:" + curDateStr + " logDate:" + logDate);


        if (curDateStr.equals(logDate)) {
            List<RateItem> retList = new ArrayList<RateItem>();
            //如果相等，则不从网络中获取数据
            Log.i(Tag, "日期相等，从数据库中获取数据");
            DBManager dbManager = new DBManager(context);
            Log.i(Tag, "从数据库中获取数据");
            for (RateItem rateItem : dbManager.listAll()) {
                String cname = rateItem.getCname();
                String cval = rateItem.getCval();
                RateItem item1 = new RateItem(cname,cval);
                retList.add(item1);
            }
            Log.i(Tag, "传递消息2");
            Message msg = handler.obtainMessage(6, retList);
//            msg.what = 6;
//            msg.obj = list1;
            Log.i(Tag, "传递消息");
            handler.sendMessage(msg);
            Log.i(Tag, "睡眠1s之后传递消息");
        } else {


            Log.i(Tag, "多线程程序开始");
//        List<HashMap<String,String>> list2 = new ArrayList<HashMap<String,String>>();
            List<RateItem> item = new ArrayList<RateItem>();
            URL url = null;


            try {

//                Document doc2 = Jsoup.connect("https://www.usd-cny.com/bankofchina.htm").get();
                Document doc2 = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
                Log.i(Tag, "run:" + doc2.title());
                Elements tables = doc2.getElementsByTag("table");
                Element table1 = tables.get(1);
                Elements hrefs = table1.getElementsByTag("tr");
                for (int i = 1; i < hrefs.size(); i++) {
                    Elements td = hrefs.get(i).getElementsByTag("td");
                    String cname = td.get(0).text();
                    String rate = td.get(5).text();
//                HashMap<String, String> map = new HashMap<>();
//                map.put("ItemTitle",cname);
//                map.put("ItemDetail",rate);
//                list1.add(cname+"——"+rate);
//                list2.add(map);
                    RateItem item1 = new RateItem(cname, rate);
                    item.add(item1);
                }


                DBManager dbManager = new DBManager(context);
                dbManager.deleteAll();
                Log.i("db","删除所有记录");
                dbManager.addAll(item);
                Log.i("db","添加新记录集");
                Log.i(Tag, "what+" + item);
            } catch (IOException e) {
                e.printStackTrace();
            }


            SharedPreferences sp = context.getSharedPreferences("myrate", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = sp.edit();
            edit.putString(DATE_SP_KEY, curDateStr);
            edit.commit();
            Log.i(Tag,"更新日期结束：" + curDateStr);

            //发送消息给主线程
            Log.i(Tag, "传递消息2");
            Message msg = handler.obtainMessage(6, item);
//            msg.what = 6;
//            msg.obj = list1;
            Log.i(Tag, "传递消息");
            handler.sendMessage(msg);
            Log.i(Tag, "睡眠1s之后传递消息");

        }




    }
}
